package com.qiangesoft.image.controller;

import com.qiangesoft.image.utils.ImageOperateUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 测试
 *
 * @author qiangesoft
 * @date 2024-04-26
 */
@RequestMapping("/image")
@RestController
public class ImageController {

    @GetMapping("/convert")
    public void convert(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片格式转换.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.convert(file, outputStream, "png");
        }
    }

    @GetMapping("/compress")
    public void compress(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片压缩.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.compress(file, outputStream, 0.4f);
        }
    }

    @GetMapping("/crop")
    public void crop(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片裁剪.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.crop(file, outputStream, 500, 400);
        }
    }

    @GetMapping("/reduceByRatio")
    public void reduceByRatio(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片比例缩小.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.reduceByRatio(file, outputStream, 3);
        }
    }

    @GetMapping("/enlargeByRatio")
    public void enlargeByRatio(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片比例放大.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.enlargeByRatio(file, outputStream, 2);
        }
    }

    @GetMapping("/resize")
    public void resize(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片宽高重置.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.resize(file, outputStream, 1000, 500);
        }
    }

    @GetMapping("/gray")
    public void gray(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片灰化.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.gray(file, outputStream);
        }
    }

    @GetMapping("/watermarkText")
    public void watermarkText(@RequestPart MultipartFile file, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片文字水印.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.watermarkText(file, outputStream, new Font("宋体", Font.BOLD, 150),
                    new Color(255, 255, 255, 255), 300, 500, "哈哈哈哈");
        }
    }

    @GetMapping("/watermarkImage")
    public void watermarkImage(@RequestPart MultipartFile file, @RequestPart MultipartFile appendFile, HttpServletResponse response) throws IOException {
        settingResponse(response, "图片图片水印.png");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageOperateUtil.watermarkImage(file, appendFile, outputStream, 0, 0, 300, 500);
        }
    }

    private static void settingResponse(HttpServletResponse response, String fileName) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
    }

}
